package qf.controller;

import com.alibaba.fastjson.JSON;
import qf.dao.TypeDao;
import qf.dao.daoImpl.TypeDaoImpl;
import qf.pojo.Type;
import qf.service.serviceImpl.TypeServiceImpl;
import qf.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 86198
 * @version V1.0
 * @Project Java_qf
 * @Package qf.controller
 * @Description:
 * @Date 2022/5/24 10:41
 */
@WebServlet("/type")
public class TypeController extends BaseServlet{
    /**
     * 查询所有商品分类
     * @param request
     * @param response
     * @return
     */
    public String findGoods(HttpServletRequest request, HttpServletResponse response){
        TypeDao typeDao = new TypeDaoImpl();
        TypeServiceImpl typeService = new TypeServiceImpl(typeDao);

        List<Type> types = typeService.findeGoods();
        request.setAttribute("goodsTypeList",types);
        return Constants.FORWARD +"/admin/showGoodsType.jsp";
    }
    /**
     *查找所有商品类型数据放到添加商品栏内
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response){
        TypeDao typeDao = new TypeDaoImpl();
        TypeServiceImpl typeService = new TypeServiceImpl(typeDao);

        List<Type> types = typeService.findeGoods();
        String jsonStr = JSON.toJSONString(types);

        try {
            response.getWriter().println(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
